<template>
	<el-container>
		<div v-show="screenVisible" class="table-screen-column">
			<el-form label-width="auto" label-position="right">
				<el-row :gutter="20">
					<el-col :span="4" >
					  	<el-form-item label="省份">
							<el-input v-model="search.province" placeholder="省份" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="4" >
					  	<el-form-item label="城市">
							<el-input v-model="search.city" placeholder="城市" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="4" >
					  	<el-form-item label="区县">
							<el-input v-model="search.district" placeholder="区县" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="4" >
					  	<el-form-item label="乡镇">
							<el-input v-model="search.town" placeholder="乡镇" clearable></el-input>
						</el-form-item>
					</el-col>
				  	<el-col :span="4" >
				  		<el-form-item label="状态">
							<el-select v-model="search.status" clearable placeholder="状态搜索">
  							  <el-option v-for="item in statusLabel"
  							    :key="item.value" :label="item.label" :value="item.value">
  							  </el-option>
  							</el-select>
						</el-form-item>
				  	</el-col>
					<el-col :span="2" >
					  	<el-form-item label="ID">
							<el-input v-model="search.id" placeholder="地址ID" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="2" >
					  	<el-form-item label="PID">
							<el-input v-model="search.parent_id" placeholder="地址PID" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="4" >
					  	<el-form-item label="简拼">
							<el-input v-model="search.jianpin" placeholder="简拼" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="4" v-if="provinceOption">
					  	<el-form-item label="区域">
							<el-select v-model="search.province_id" placeholder="选项" @change="regionOptionProv">
  							  <el-option v-for="item in provinceOption" :key="item.id" :label="item.name" :value="item.id" />
  							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="4" v-if="cityOption">
					  	<el-form-item label=" ">
							<el-select v-model="search.city_id" placeholder="选项" @change="regionOptionCity">
  							  <el-option v-for="item in cityOption" :key="item.id" :label="item.name" :value="item.id" />
  							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="4" v-if="districtOption">
					  	<el-form-item label=" ">
							<el-select v-model="search.district_id" placeholder="选项" @change="regionOptionDist">
  							  <el-option v-for="item in districtOption" :key="item.id" :label="item.name" :value="item.id" />
  							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="4" v-if="townOption">
					  	<el-form-item label=" ">
							<el-select v-model="search.town_id" placeholder="选项">
  							  <el-option v-for="item in townOption" :key="item.id" :label="item.name" :value="item.id" />
  							</el-select>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
		</div>
		
		<el-header>
			<div class="left-panel">
                <el-button icon="el-icon-plus" type="primary" plain @click="add">新增</el-button>
                <el-button plain icon="el-icon-upload" @click="importData">导入</el-button>
			</div>
			<div class="right-panel">
				<div class="right-panel-search">
					<el-input v-model="search.name" placeholder="地名" clearable></el-input>
					<el-input v-model="search.shortname" placeholder="地名简称" clearable></el-input>
					<el-button type="primary" icon="el-icon-search" plain @click="handleQuery"></el-button>
					<el-button @click="resetQuery" icon="el-icon-refresh">重置</el-button>
					<el-button type="primary" link @click="screenVisible = !screenVisible">
            		  {{ screenVisible ? '关闭更多筛选' : '显示更多筛选'}}
            		  <el-icon><el-icon-arrow-down v-if="screenVisible" /><el-icon-arrow-up v-else /></el-icon>
            		</el-button>
				</div>
			</div>
		</el-header>
		
		<el-main class="nopadding">
			<el-table ref="table" v-loading="loading" :data="tableList" @selection-change="selectionChange" border stripe>
				<el-table-column type="expand">
					<template #default="props">
						<div class="table-expand-column">
							<el-row :gutter="20">
								<el-col :span="4">
									PID：{{props.row.parent_id}}
								</el-col>
								<el-col :span="4">
									拼音：{{props.row.pinyin}}
								</el-col>
								<el-col :span="4">
									简拼：{{props.row.jianpin}}
								</el-col>
								<el-col :span="4">
									首字母：{{props.row.firstchar}}
								</el-col>
								<el-col :span="8">
									全称：{{props.row.fullname}}
								</el-col>
							</el-row>
						</div>
					</template>
				</el-table-column>
				<el-table-column label="#" prop="id" width="80"></el-table-column>
				<el-table-column label="地名" prop="name"></el-table-column>
				<el-table-column label="省份" prop="province"></el-table-column>
				<el-table-column label="城市" prop="city"></el-table-column>
				<el-table-column label="区/县" prop="district"></el-table-column>
				<el-table-column label="乡/镇" prop="town"></el-table-column>
				<el-table-column label="简称" prop="shortname"></el-table-column>
				<el-table-column label="深度" prop="depth"  width="80"></el-table-column>
				<el-table-column label="状态" prop="status" width="100">
					<template #default="scope">
						<el-tag v-if="scope.row.status==1" type="success">正常</el-tag>
						<el-tag v-if="scope.row.status==0" type="danger">禁用</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="操作" fixed="right" align="center" width="120">
					<template #default="scope">
						<el-button-group>
							<el-button text type="primary" size="small" @click="table_show(scope.row, scope.$index)">查看</el-button>
							<el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)">编辑</el-button>
						</el-button-group>
					</template>
				</el-table-column>
			</el-table>
		</el-main>
		<el-footer>
			<sc-page v-model="search"></sc-page>
		</el-footer>
	</el-container>

	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess" @closed="dialog.save=false"></save-dialog>
	<import-dialog v-if="dialog.import" ref="importDialog" @success="handleSaveSuccess" @closed="dialog.import=false"></import-dialog>

</template>

<script>
	import saveDialog from './save'
	import importDialog from './import'

	export default {
		name: 'developDistrict',
		components: {
			saveDialog, importDialog
		},
		data() {
			return {
				dialog: {
					save: false, import: false
				},
				loading: true,
				screenVisible: false,
				tableList: [],
				selection: [],
				//行政区域选项
				provinceOption: [],
				cityOption: null,
				districtOption: null,
				townOption: null,
				//查询条件
				search: {
					page: 1,
        			pageSize: 20,
					total: 0,
					paging: 0,
        			name: undefined,
        			shortname: undefined,
        			province: undefined,
        			city: undefined,
        			district: undefined,
        			town: undefined,
        			status: undefined,
        			jianpin: undefined,
        			id: undefined,
        			parent_id: undefined,
        			province_id: undefined,
        			city_id: undefined,
        			district_id: undefined,
        			town_id: undefined,
				},
				statusLabel: [
					{value: 0, label: '禁用'},
					{value: 1, label: '正常'},
				],
			}
		},
		mounted() {
			this.handleQuery()
			this.regionOptionChange(0)
		},
		watch: {
        	"search.paging": function() {
				this.handleQuery()
			},
		},
		methods: {
			//获取列表
			async getTableList() {
				this.loading = true
				var res = await this.$API.develop.district.list.get(this.search)
				this.loading = false
				if(res.code == 200) {
					this.tableList = res.data.rows
					this.search.total = res.data.total
				} else {
					this.$alert(res.message, "提示", {type: 'error'})
				} 
			},
			//搜索
			handleQuery() {
				this.getTableList()
			},
			//重置搜索
			resetQuery() {
				this.search = {
					page: 1,
        			name: undefined,
        			shortname: undefined,
        			province: undefined,
        			city: undefined,
        			district: undefined,
        			town: undefined,
        			status: undefined,
        			jianpin: undefined,
        			id: undefined,
        			parent_id: undefined,
        			province_id: undefined,
        			city_id: undefined,
        			district_id: undefined,
        			town_id: undefined,
				}
				this.cityOption = null
				this.districtOption = null
				this.townOption = null
				this.handleQuery()
			},
			//添加
			add() {
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('add', this.search)
				})
			},
			//导入
            importData() {
				this.dialog.import = true
				this.$nextTick(() => {
					this.$refs.importDialog.open('add', this.search)
				})
            },
			//编辑
			table_edit(row){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('edit', this.search).setData(row)
				})
			},
			//查看
			table_show(row){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('show').setData(row)
				})
			},
			//删除
			async table_del(row){
				var reqData = {id: row.id}
				var res = await this.$API.agent.users.delete.post(reqData);
				if(res.code == 200){
					this.handleQuery()
					this.$message.success("删除成功")
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
			//批量删除
			async batch_del(){
				this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？如果删除项中含有子集将会被一并删除`, '提示', {
					type: 'warning'
				}).then(() => {
					const loading = this.$loading();
					this.handleQuery()
					loading.close();
					this.$message.success("操作成功")
				}).catch(() => {

				})
			},
			//表格选择后回调事件
			selectionChange(selection){
				this.selection = selection;
			},
			//区域选项
			regionOptionProv(e) {
				this.districtOption = null
				this.townOption = null
				this.search.city_id = undefined
				this.search.district_id = undefined
				this.search.town_id = undefined
				this.regionOptionChange(1, e)
			},
			regionOptionCity(e) {
				this.townOption = null
				this.search.district_id = undefined
				this.search.town_id = undefined
				this.regionOptionChange(2, e)
			},
			regionOptionDist(e) {
				this.search.town_id = undefined
				this.regionOptionChange(3, e)
			},
			async regionOptionChange(option, parentId) {
				var res = await this.$API.develop.district.option.get({parent_id: parentId});
				if(res.code == 200) {
					let list = res.data
					if(list.length > 0) {
						if(option == 0) {
							this.provinceOption = list
						} else if(option == 1) {
							this.cityOption = list
						} else if(option == 2) {
							this.districtOption = list
						} else if(option == 3) {
							this.townOption = list
						}
					}
				} else {
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
			//本地更新数据
			handleSaveSuccess(mode, search){
				if(mode !='show') {
					this.search = search
					this.handleQuery()
				}
			},
		}
	}
</script>

<style>
  .el-row {
    margin-bottom: 6px;
    &:last-child { margin-bottom: 0;}
  }
  .el-col {border-radius: 4px;}
  .bg-purple-dark {background: #99a9bf;}
  .bg-purple {background: #d3dce6;}
  .bg-purple-light {background: #e5e9f2;}
  .grid-content {border-radius: 4px;min-height: 36px;}
  .row-bg {padding: 10px 0;background-color: #f9fafc;}
  .table-expand-column {padding: 0 20px;}
</style>
